package cn.itcast.manus.service.impl;


import cn.hutool.core.util.StrUtil;
import cn.itcast.manus.agent.Agent;
import cn.itcast.manus.agent.AgentFactory;
import cn.itcast.manus.dto.DialogMessageDTO;
import cn.itcast.manus.enums.AgentTypeEnum;
import cn.itcast.manus.message.MessageSession;
import cn.itcast.manus.message.WSSessionManagement;
import cn.itcast.manus.service.WebSocketService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Service
@RequiredArgsConstructor
public class WebSocketServiceImpl implements WebSocketService {

    private final WSSessionManagement wsSessionManagement;
    private final ExecutorService executor = Executors.newCachedThreadPool();

    @Override
    public void enhancedDialog(DialogMessageDTO message, SimpMessageHeaderAccessor headerAccessor) {
        if (StrUtil.isEmpty(message.getText())) {
            log.info("receive empty message!!!");
            return;
        }

//        获取会话id
        String sessionId = headerAccessor.getSessionId();
//        根据会话id获取对话对象
        MessageSession wsSession = this.wsSessionManagement.sessionDialog(sessionId);
//        将消息对象写入到会话中
        wsSession.receiveMessages(message);

//        通过智能体进行处理业务
        Agent agent = AgentFactory.getAgent(AgentTypeEnum.RE_ACT_PLANNING_AGENT).apply(wsSession);
        Runnable src = () -> {
            try {
//            获取客户端发来的消息
                String rawInput = wsSession.readMessage();
//            通过规划agent进行执行
                String finalResult = agent.solveTask(rawInput);
                log.info("complete with final result:{}", finalResult);
                wsSession.sendMessage(finalResult);
                wsSession.sendMessage("任务流程结束");
            } finally {
                wsSessionManagement.clean(sessionId);
            }
        };
        executor.execute(src);
    }
}
